import axios from 'axios'
import md5 from 'js-md5';
let apiUrl = "https://api.fengwobang.com";

export default {
    install: (Vue, options) => {
        Vue.prototype.host = apiUrl,
        Vue.prototype.ajax = function (option) {
            var that = this;
            if (typeof (option.load) == 'object') { option.load.load = true; } //加载效果
            if (option.changeUrl && option.params) { that.$router.replace({ query: option.params }); } //在某种情况下需要修改url，比如翻页！
            return axios({
                method: option.method || 'get',
                url: option.url || '/',
                baseURL: option.base_url || that.host,
                data: option.data || {},
                params: option.params || {},
                headers: option.header || {
                    //'Content-Type':'application/x-www-form-urlencoded'
                    'authorizedkey': localStorage.getItem('authorized_key')
                },
                responseType: option.dataType || 'json', // 默认json
            }).then(function (res) {
                if (res.data.code == 0) {
                    option.success && option.success(res.data.data);
                } else if (res.data.code == 401) {
                    /*that.$confirm({
                        title: '提示',
                        content: '您的登录好像过期了，是否重新登录？',
                        okText: '确认',
                        cancelText: '取消',
                        onOk() {
                            localStorage.removeItem('authorized_key');
                            localStorage.removeItem('name');
                            that.$router.push({ path: '/' })
                        }
                    });*/
                } else {
                    if (option.error) {
                        option.error(res.data);
                    } else {
                        //that.$message.error("错误代码：" + res.data.code + "，信息：" + res.data.message + (res.data.data.info ? "，原因：" + res.data.data.info : ""));
                    }
                }
                option.done && option.done(res);
                if (typeof (option.load) == 'object') { option.load.load = false; }
            }).catch(function (err) {
                that.$message.error("服务器网络异常！");
                option.done && option.done(err);
                if (typeof (option.load) == 'object') { option.load.load = false; }
            })
        },
        Vue.prototype.parseMoney = function (num, digit = 2) {
            return (num / 10000).toFixed(digit);
        },
        Vue.prototype.parseServerMoney = function (num) {
            return (num * 10000);
        },
        Vue.prototype.isFile = function (name) {
            if (name) {
                var index = name.lastIndexOf(".");
                var ext = name.substr(index + 1);
                if (['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp'].indexOf(ext.toLowerCase()) !== -1) {
                    return "photo";
                } else if (['mp4', 'mov', 'flv', 'fly'].indexOf(ext.toLowerCase()) !== -1) {
                    return "video";
                } else {
                    return "file";
                }
            } else {
                return "photo";
            }
        }
    }
}